<template>
  <div>
    <div class="prefix" v-if="hotCities">
      <header>热门城市</header>
      <div
        class="hotcity"
        v-for="city in hotCities.cities"
        :key="city.cityId"
        @click="chooseCity(city)"
      >
        {{ city.name }}
      </div>
    </div>

    <div class="prefix" v-for="item in otherCities" :key="item.prefix">
      <header>{{ item.prefix | toUpperCase }}</header>
      <div
        class="city"
        v-for="city in item.cities"
        :key="city.cityId"
        @click="chooseCity(city)"
      >
        {{ city.name }}
      </div>
    </div>
  </div>
</template>

<script>
// import { getCityListApi } from "@/utils/api";
import { mapMutations, mapGetters, mapActions } from "vuex";

export default {
  computed: mapGetters("city", ["hotCities", "otherCities"]),
  mounted() {
    this.getCityListAsync();
  },
  methods: {
    ...mapMutations("city", ["changeCity"]),
    ...mapActions("city", ["getCityListAsync"]),
    chooseCity(city) {
      // 存到vuex
      this.changeCity(city);
      // 存到本地存储
      localStorage.setItem("cityname", city.name);
      localStorage.setItem("cityId", city.cityId);
      this.$router.go(-1);
    },
  },
  filters: {
    toUpperCase(val) {
      return val.toUpperCase();
    },
  },
};
</script>

<style lang="less" scoped>
.prefix {
  line-height: 30px;
  background-color: #ebebeb;
  font-size: 14px;
  color: #333;

  header {
    padding-left: 25px;
  }

  .city {
    height: 44px;
    line-height: 44px;
    border-bottom: 1px solid #c8c7cc;
    background: #fff;
  }

  .hotcity {
    float: left;
    background: #fff;
    width: 29%;
    height: 33px;
    margin-top: 15px;
    margin-left: 4%;
    padding: 0 4px;
    border: 1px solid #e6e6e6;
    border-radius: 3px;
    line-height: 33px;
    text-align: center;
    box-sizing: border-box;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
</style>
